//
//  EditInfoViewController.m
//  xxShare
//
//  Created by 柳麟喆 on 2018/3/27.
//  Copyright © 2018年 lzLiu. All rights reserved.
//

#import "EditInfoViewController.h"
#import "UIView+LLZHUD.h"

@interface EditInfoViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@end

@implementation EditInfoViewController

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"编辑资料";
    
    //添加保存按钮
    UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveInfo)];
    self.navigationItem.rightBarButtonItem = saveBtn;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)saveInfo{
    NSDictionary *dic = @{@"avator":_avator.image,@"name":_name.text,@"introduce":_introduce.text};
    //发送通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"EditNotification" object:dic];
    [SVProgressHUD showSuccessWithStatus:@"保存成功"];
    [SVProgressHUD dismissWithDelay:0.8 completion:^{
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (IBAction)avatorClicked:(UITapGestureRecognizer *)sender {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        // 设置 sourceType
        imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        imagePicker.delegate = self;
        // 允许对图片进行拉伸、裁剪等编辑操作，如果设置为 NO，则直接使用原图
        imagePicker.allowsEditing = YES;
        [self presentViewController:imagePicker animated:YES completion:nil];
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            // 设置 sourceType
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePicker.delegate = self;
            // 允许对图片进行拉伸、裁剪等编辑操作，如果设置为 NO，则直接使用原图
            imagePicker.allowsEditing = YES;
            [self presentViewController:imagePicker animated:YES completion:nil];
        }else {
            [self.view showAutoDismissHUD:@"相机无法使用！"];
        }
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - *******UIImagePickerControllerDelegate*******

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    _avator.image = [info objectForKey:UIImagePickerControllerEditedImage];
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
